package com.buvaka;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.buvaka.features.*;

import org.bson.types.ObjectId;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Created by Алексей on 25.02.14.
 */
public class UpdatePoint extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        if (request.getParameter("lat") != "" && request.getParameter("lng") != "" && request.getParameter("lat") != null && request.getParameter("lng") != null
                && request.getParameter("name") != "" && request.getParameter("name") != null
                && request.getParameter("description") != "" && request.getParameter("description") != null) {
            LatLng lng1 = new LatLng(Double.parseDouble(request.getParameter("lat")), Double.parseDouble(request.getParameter("lng")));
            Point p1 = new Point(lng1);
            Feature f1 = new Feature(p1);

            try{
                MongoClient client = new MongoClient();
                DB db = client.getDB("gis");
                DBCollection points = db.getCollection("points");

                Object o = com.mongodb.util.JSON.parse(f1.toString());
                BasicDBObject geo = (BasicDBObject)o;
                geo.append("category", request.getParameter("category"));
                geo.append("name", request.getParameter("name"));
                geo.append("description", request.getParameter("description"));

                points.update(new BasicDBObject("name", request.getParameter("name")), geo);
                
                updateLinks(request.getParameter("name"), db.getCollection("links"), lng1.toArray());            
                
                UpdateGeoJsonFile.update(client);
                
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    
    /*
     * Get position of edited point in all of links where it appears
     * 
     * temporary links' collection required
     */
    
    public void updateLinks(String ID, DBCollection links, double[] address){
    	    	
        DBCursor affectedLinks = links.find(new BasicDBObject("pointsNames", ID));
                
        while (affectedLinks.hasNext()){
        	DBObject current = affectedLinks.next();	
        	BasicDBList names = (BasicDBList)current.get("pointsNames"); //to get index of the point
        	int index = names.indexOf(ID); //index
        	BasicDBList coordinates = (BasicDBList)current.get("coordinates"); // to change apropriate coordinate
        	
        	coordinates.set(index, address); // set it
        	
        	links.update(new BasicDBObject("_id", current.get("_id")), current); //update
          	        	
        }
    	
    }


}
